import cv2
import numpy as np
#
from PIL import ImageGrab
pic = ImageGrab.grab()
pic.save('1.png')
#截屏

img = cv2.imread('1.png',0)
template = cv2.imread('test.png',0)
h , w = template.shape[:2] #模板图片的宽和长
ch = h//2
cw = w//2

res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF) #只匹配一次
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
left_top = max_loc #左上角坐标
right_bottom = (left_top[0] + w,left_top[1] + h) #右下角坐标
left_center = left_top[0] + cw
right_center = left_top[1] + ch

print(left_top)
print(right_bottom)

print(left_center,right_center)
#cv2.rectangle(img, left_top, right_bottom, 255, 2)
#cv2.imshow('rgb',img)
#cv2.waitKey(0)


#所有匹配结果
#res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)
#threshold = 0.8
#loc = np.where(res >= threshold)
#for pt in zip(*loc[::-1]):
#    right_bottom = (pt[0] + w, pt[1] + h)
#    print(pt)
